package com.huaxin.common.utils;

import android.util.Log;

import com.baidu.mapapi.model.LatLng;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author lwjing
 * @date 2018/7/18
 * @Desc $desc$ 计算map中自适应level
 * Copyright HUAXIN DATA. All rights reserved.
 */
public class AdaptiveMapLevelUtil {

    private static AdaptiveMapLevelUtil adaptiveMapLevelUtil;
    private List<Double> latitudeList  = new ArrayList<Double>();
    private List<Double> longitudeList = new ArrayList<Double>();
    private double maxLatitude;
    private double minLatitude;
    private double maxLongitude;
    private double minLongitude;
    private double distance;

    //    private float level;
    public static AdaptiveMapLevelUtil getInstance() {
        if (adaptiveMapLevelUtil == null) {
            adaptiveMapLevelUtil = new AdaptiveMapLevelUtil();
        }
        return adaptiveMapLevelUtil;
    }

    public int getlevel(List<LatLng> latLngsList) {
        if (latLngsList == null || latLngsList.size() < 2) {
            return 0;
        }
        //比较选出集合中最大经纬度
        getMax(latLngsList);
        //计算两个Marker之间的距离
        calculateDistance();
        //根据距离判断地图级别
        return getLevel();
        //计算中心点经纬度，将其设为启动时地图中心
        //        setCenter();

    }

    /**
     * 比较选出集合中最大经纬度
     *
     * @param latLngsList
     */
    private void getMax(List<LatLng> latLngsList) {
        for (int i = 0; i < latLngsList.size(); i++) {
            double latitude = latLngsList.get(i).latitude;
            double longitude = latLngsList.get(i).longitude;
            latitudeList.add(latitude);
            longitudeList.add(longitude);
        }
        maxLatitude = Collections.max(latitudeList);
        minLatitude = Collections.min(latitudeList);
        maxLongitude = Collections.max(longitudeList);
        minLongitude = Collections.min(longitudeList);
    }


    /**
     * 计算两个Marker之间的距离
     */
    private void calculateDistance() {
        distance = GeoHasher.GetDistance(maxLatitude, maxLongitude, minLatitude, minLongitude);
    }

    /**
     * 根据距离判断地图级别
     */
    private int getLevel() {
        int level = 0;
        int zoom[] = {10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 1000, 2000, 25000, 50000, 100000, 200000, 500000, 1000000, 2000000};
        Log.i("info", "maxLatitude==" + maxLatitude + ";minLatitude==" + minLatitude + ";maxLongitude==" + maxLongitude + ";minLongitude==" + minLongitude);
        Log.i("info", "distance==" + distance);
        for (int i = 0; i < zoom.length; i++) {
            int zoomNow = zoom[i];
            if (zoomNow - distance * 1000 > 0) {
                level = 18 - i + 6;
                //设置地图显示级别为计算所得level
                //                mBaiduMap.setMapStatus(MapStatusUpdateFactory.newMapStatus(new MapStatus.Builder().zoom(level).build()));
                break;
            }
        }
        return level;
    }

    /**
     * 计算中心点经纬度，将其设为启动时地图中心
     */
    public LatLng getCenter() {
        LatLng center = new LatLng((maxLatitude + minLatitude) / 2, (maxLongitude + minLongitude) / 2);
        Log.i("info", "center==" + center);
        return center;
        //            MapStatusUpdate status1 = MapStatusUpdateFactory.newLatLng(center);
        //            mBaiduMap.animateMapStatus(status1, 500);
    }
}
